<?php

class User_Model extends MyModel
{
    
    function __construct()
    {
        parent::__construct();
    }
    
    function getUser($id)
    {
        $str = "SELECT username, password, email, role 
                FROM user 
                WHERE id = '{$id}'";
                
        $query = mysql_query($str);
        return $query;
    }
    
    function getAllUser()
    {
        $str = "SELECT username, password, email, role
                FROM user";
                
        $query = mysql_query($str);
        return $query;        
    }
    
    
    function checkExist($action, $username, $password)
    {
        switch($action)
        {
            case "login":
                $str = "SELECT * 
                        FROM user 
                        WHERE username = '{$username}' AND password = '{$password}'";
                        
                $query = mysql_query($str);
                $result = mysql_num_rows($query) > 0 ? true : false;
                break;
                
            case "add":
                $str = "SELECT username 
                        FROM user 
                        WHERE username = '{$username}'"; 
                $query = mysql_query($str);
                $result = mysql_num_rows($query) > 0 ? true : false;
                break;
        }
        return $result;
    }
    
    function addUser($username, $password, $email)
    {
        $str = "INSERT INTO user 
                VALUES('{$username}', '{$password}', '{$email}')";
                
        mysql_query($str);
    }
    
    function editUser($id, $password, $email)
    {
        if($password == "")
        {
            $str = "UPDATE user 
                    SET email = '{$email}'  
                    WHERE id = '{$id}'";
        }
        if($email == "")
        {
            $str = "UPDATE user 
                    SET password = '{$password}'  
                    WHERE id = '{$id}'";
        }
        if($password != "" && $email != "")
        {
            $str = "UPDATE user 
                    SET password = '{$password}', email = '{$email}'  
                    WHERE id = '{$id}'";
        }
              
        mysql_query($str);
    }
    
    
    function deleteUser($id)
    {
        $str = "DELETE FROM user 
                WHERE id = '{$id}'";
                
        mysql_query($str);
    }
    
}

?>